package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.pojo.Item;
import com.hmall.common.vo.PageVO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public PageDTO<Item> pageQueryItem(PageVO pageVO) {
        //创建分页查询条件
        IPage<Item> page=new Page<>(pageVO.getPage(),pageVO.getSize());
        //调用分页查询方法
        IPage<Item> itemIPage = itemMapper.selectPage(page, null);
        //创建返回对象
        PageDTO<Item> pageDTO=new PageDTO<>();
        pageDTO.setTotal(itemIPage.getTotal());
        pageDTO.setList(itemIPage.getRecords());
        //返回DTO
        return pageDTO;
    }

    @Override
    public Item findItemById(Long id) {
        Item item = itemMapper.selectById(id);
        return item;
    }

    @Override
    public void saveItem(Item item) {
        itemMapper.insert(item);
    }

    @Override
    public void updateItemStatus(Long id, Integer status) {
        //发送消息
        String queueName="item.queue";
        Item item=Item.builder().id(id).status(status).build();
        itemMapper.updateById(item);
        //发送消息
        rabbitTemplate.convertAndSend(queueName,item);
    }

    @Override
    public void updateItem(Item item) {
        itemMapper.updateById(item);
    }


}
